import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wanxiang_app/bean/simple_entity.dart';
import 'package:wanxiang_app/utils/constant.dart';
import 'package:wanxiang_app/widgets/money_textField.dart';
import '../../utils/network_manager.dart';
import '../../utils/network_url.dart';
import '../../utils/toast_util.dart';
import '../../utils/user_util.dart';
import '../../widgets/account_textField2.dart';
import '../../widgets/myappbar.dart';
import 'package:get/get.dart';
import 'package:tobias/tobias.dart' ;

class PayController extends GetxController{


  String price = '';
  Map payMap = {};
  bool inputEnable = false;
  @override
  void onInit() {
    super.onInit();

  }

  bool checked = true;
  changeCheck(){
    checked = !checked;
    update();
  }

  int clickIndex = 0;
  @override
  void onClose() {
    super.onClose();
  }
  String inputPrice = '';

  void _pay(){
      _buyNow();
  }

  void _buyNow(){
    print('_buyNow_buyNow_buyNow');
    DioManager.getInstance().post(ServiceUrl.getAddRecharge,{
      "recharge_price":inputPrice.toString(),
      "user_name":UserUtil.getUSER()?.userName.toString(),
    }, (data) async {
      // SimpleEntity bannerEntity = SimpleEntity.fromJson(jsonDecode(data));
      // ToastUtil.show(bannerEntity.msg);

      Tobias tobias = Tobias();
      tobias.pay(data);
    }, (error) {
      ToastUtil.show(error);
    });
  }


}

class TopUpPage extends GetView<PayController>  {

  late PayController controller ;
  late BuildContext context ;
  @override
  Widget build(BuildContext context1) {
    Get.lazyPut<PayController>(() => PayController());
    controller = Get.find();
    context = context1;

    return GetBuilder<PayController>(
      builder: (_){
        return  Scaffold(
            appBar: MyAppBar(context: context, title: '充值',hasLeading: true,),
            backgroundColor: Colors.white,
            body:_content());
      },
    );
  }

  Widget _content(){
    return SingleChildScrollView(
      child: Container(
        padding: EdgeInsets.only(left: 20.w,right: 27.w),
        child: Column(
          children: [
            SizedBox(height: 24.h,),
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Text('充值金额',style: TextStyle(color: Color(0xFF333333),fontSize: 15.sp),)
              ],
            )
            ,
            Container(
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(6.r),
                  color: Color(0xFFF9F9F9)
              ),
              height: 48.h,
              margin: EdgeInsets.only(
                top: 15.h,
              ),
              child:MoneyTextField(
                //获取返回的输入内容
                readOnly: false,
                hitText: '请输入金额',
                inputColor: 0xFFF9F9F9,
                contentStrCallBack: (content) {
                  controller.inputPrice = content;
                }, key: ValueKey('money'),
              ),
            ),
            SizedBox(height: 28.h,),
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Text('支付方式',style: TextStyle(color: Color(0xFF333333),fontSize: 15.sp),)
              ],
            )
            ,
            Container(
              margin: EdgeInsets.only(top: 14.h),
              padding: EdgeInsets.only(left: 20.w,right: 22.w),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(6.r),
                  color: Color(0xFFF9F9F9)
              ),
              child: Column(
                children: [
                  // _item(Constant.IMG_BASE+'icon_wx.png','微信',0),
                  _item(Constant.IMG_BASE+'icon_zfb.png','支付宝',1),
                  // _item(Constant.IMG_BASE+'icon_balance.png','余额',2),
                  SizedBox(height: 36.h,)
                ],
              ),
            ),
            _btn()

          ],
        ),
      )
    ) ;
  }
  Widget _item(img,name,index){
    return Container(
      margin: EdgeInsets.only(top: 23.h),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              Image.asset(img,width: 34.w,height: 34.h,),
              SizedBox(width: 12.w,),
              Text(name,style: TextStyle(color: Color(0xFF333333),fontSize: 14.sp,fontWeight: FontWeight.bold),),
            ],
          ),
          InkWell(
            onTap: (){
             controller.clickIndex = index;
             controller.update();
            },
            child:    controller.clickIndex==index?
            Image.asset(Constant.IMG_BASE+'radio_y.png',width: 14.w,height: 14.h,)
                :
            Image.asset(Constant.IMG_BASE+'radio_n.png',width: 14.w,height: 14.h,)
          )

        ],
      ),
    );
  }

  Widget _btn(){
    return InkWell(
        onTap: () async {
          Tobias tobias = Tobias();
          var result = await tobias.isAliPayInstalled;
          if(result){
            if(controller.inputPrice.isEmpty){
              ToastUtil.show('请输入金额');
              return;
            }
            controller._pay();
          }else{
            ToastUtil.show('请先安装支付宝');
          }

        },
        child:Container(
          margin: EdgeInsets.only(top: 30.h),
          height: 42.h,
          width: MediaQuery.of(context).size.width - 64.w,
          alignment: Alignment.center,
          decoration: BoxDecoration(
              color: Color(0xFF007BE5),
              borderRadius: BorderRadius.circular(22.r)
          ),
          child: Text('确定',style: TextStyle(
              fontSize: 14.sp,
              color: Colors.white
          ),),
        )
    );
  }

}
